<?php
function dmodel_new_class($query,$data=array()) {
	$a = dmodel_get_args($query,"name");	
	$p = dmodel_get_prefix($a);
	
	$d = new stdClass();
	$d->form = dmodel_get_class_form($a->form,$data);
	$d->name = $a->name;
	$d->mood = 1;
	
	if (!dmodel_hit_class($query,$data)) {	
		if (!dmodel_hit_class($query)) {
			# class not exist by name create it
			$i = dmodel_sql_insert($d,"class");
			$q = "INSERT INTO {$p}class({$i->fields}) VALUES ({$i->values})";
			if ($a->get == "query") {return $q;}
			dmodel_query($q);		
		} else {
			# class exist by name update it definition
			$q = "UPDATE {$p}class SET form='{$d->form}' WHERE name='{$d->name}'";
			if ($a->get == "query") {return $q;}
			dmodel_query($q);
		}
	}
}

function dmodel_hit_class($query="",$data=array()) {
	$a = dmodel_get_args($query,"name");
	$p = dmodel_get_prefix($a);
	
	$d = new stdClass();
	$d->form = dmodel_get_class_form($a->form,$data);
	$d->name = $a->name;
	
	if ($d->form) {
		$q = "SELECT id FROM {$p}class WHERE name='{$d->name}' AND form='{$d->form}' LIMIT 1";
	} else {
		$q = "SELECT id FROM {$p}class WHERE name='{$d->name}' LIMIT 1";
	}
	
	if ($a->get == "query") {
		return $q;
	} else {
		$r = dmodel_query($q);	
		return $r;
	}
}

function dmodel_get_class($query="",$data=null) {
	$a = dmodel_get_args($query,"name");
	$p = dmodel_get_prefix();
	
	if ($a->name!="") {
		$q = "SELECT * FROM {$p}class WHERE name='{$a->name}' LIMIT 1";
	} else {
		$q = "SELECT * FROM {$p}class";
	}
	
	if ($a->get == "query") {
		return $q;
	} else {
		$r = dmodel_query($q);
		if ($r) {
			#return first 
			$r = $r[0];
			$r->form = dmodel_parse_class_form($r->form);
			return $r;
		} else {
			$r = new stdClass();
			$r->name = "null";
			$r->form = array();
			return $r;
		}
	}
}

function dmodel_parse_class_form($form) {
	#$form = 'id as id:0;'.$form;
	$o = array();	
	$temp = explode(";",$form);
	foreach($temp as $line) {
		if (strpos($line,":")) {
			$line = explode(":",$line);
			$line[0] = explode(" as ",$line[0]);
			$star = (substr($line[0][0],-1)=="*") ? true : false;
			$other = substr($line[0][0],-1);
			if ($star) {
				$line[0][0] = substr($line[0][0],0,strlen($line[0][0])-1);
			}
			$space = isset($line[1]) ? explode(",",$line[1]) : array();
			$depes = array();
			foreach($space as $s) {
				preg_match_all("/([a-z][a-z0-9]*?\.[a-z][a-z0-9]*|[a-z][a-z0-9]*)/",$s,$a);
				$a = $a[1];
				preg_match_all("/([a-z][a-z0-9]*)[ ]*\(/",$s,$b);
				$b = $b[1];			
				$c = array_diff($a,$b);
				foreach($c as $d) {
					if($d) {$depes[]=$d;}
				}
			}
			$o[] = array(
				"field" => isset($line[0][1]) ? $line[0][1] : "none",
				"class" => $line[0][0],
				"space" => $space,
				"group" => (dmodel_is_object_standard_field($line[0][0])) ? "field" : 
						   (dmodel_is_object_meta_field($line[0][0])) ? "meta" : "class",
				"array" => ($star) ? true : false,		
				"depes" => $depes,
				"parse" => false,
			);
		} else if (strpos($line,"=")) {
			$line = explode("=",$line);
			$line[0] = explode(" as ",$line[0]);
			$star = (substr($line[0][0],-1)=="*") ? true : false;
			$other = substr($line[0][0],-1);
			if ($star) {
				$line[0][0] = substr($line[0][0],0,strlen($line[0][0])-1);
			}
			$space = isset($line[1]) ? explode(",",$line[1]) : array();
			$depes = array();
			foreach($space as $s) {
				preg_match_all("/([a-z][a-z0-9]*?\.[a-z][a-z0-9]*|[a-z][a-z0-9]*)/",$s,$a);
				$a = $a[1];
				preg_match_all("/([a-z][a-z0-9]*)[ ]*\(/",$s,$b);
				$b = $b[1];			
				$c = array_diff($a,$b);
				foreach($c as $d) {
					if($d) {$depes[]=$d;}
				}
			}
			$o[] = array(
				"field" => isset($line[0][1]) ? $line[0][1] : "none",
				"class" => $line[0][0],
				"space" => $space,
				"group" => (dmodel_is_object_standard_field($line[0][0])) ? "field" : 
						   (dmodel_is_object_meta_field($line[0][0])) ? "meta" : "class",
				"array" => ($star) ? true : false,		
				"depes" => $depes,
				"parse" => true,
			);
		} else {
			$line = array($line);
			$line[0] = explode(" as ",$line[0]);
			$star = (substr($line[0][0],-1)=="*") ? true : false;
			$other = substr($line[0][0],-1);
			if ($star) {
				$line[0][0] = substr($line[0][0],0,strlen($line[0][0])-1);
			}
			$space = isset($line[1]) ? explode(",",$line[1]) : array();
			$depes = array();
			foreach($space as $s) {
				preg_match_all("/([a-z][a-z0-9]*?\.[a-z][a-z0-9]*|[a-z][a-z0-9]*)/",$s,$a);
				$a = $a[1];
				preg_match_all("/([a-z][a-z0-9]*)[ ]*\(/",$s,$b);
				$b = $b[1];			
				$c = array_diff($a,$b);
				foreach($c as $d) {
					if($d) {$depes[]=$d;}
				}
			}
			$o[] = array(
				"field" => isset($line[0][1]) ? $line[0][1] : "none",
				"class" => $line[0][0],
				"space" => $space,
				"group" => (dmodel_is_object_standard_field($line[0][0])) ? "field" : 
						   (dmodel_is_object_meta_field($line[0][0])) ? "meta" : "class",
				"array" => ($star) ? true : false,		
				"depes" => $depes,
				"parse" => false,
			);
		}
	}
	return $o;
}

function dmodel_get_class_form($form,$data) {
	$line = array();
	foreach($data as $a=>$b) {
		if (is_string($a)) {
			if (is_array($b)) {
				if (strpos($a," as ")>0) {
					$line[] = $a.':'.implode(",",$b);										
				} else {
					$line[] = dmodel_field_suggest($a).' as '.$a.':'.implode(",",$b);								
				}		
				
			} else if (strpos($b,",")) {
				if (strpos($a," as ")>0) {
					$line[] = $a.':'.$b;										
				} else {
					$line[] = dmodel_field_suggest($a).' as '.$a.':'.$b;								
				}		
				
			} else {
				if (strpos($a," as ")>0) {
					$line[] = $a.'='.$b;				
						
				} else {
					$line[] = dmodel_field_suggest($a).' as '.$a.'='.$b;			
					
				}		
			}
		} else {
			if (strpos($b," as ")>0) {
				$line[] = $b;
				
			} else {
				if (strpos($b,"=")>0) {
					$b = explode("=",$b,2);
					$line[] = dmodel_field_suggest(trim($b[0])).' as '.trim($b[0]).'='.trim($b[1]);			
					
				} else if (strpos($b,":")>0) {
					$b = explode(":",$b,2);
					$line[] = dmodel_field_suggest(trim($b[0])).' as '.trim($b[0]).':'.trim($b[1]);			
					
				} else {
					$line[] = dmodel_field_suggest(trim($b)).' as '.trim($b);	
					
				}				
			}					
		}
	}
	$form = ($form ? $form.';' : '').implode(";",$line);
	return $form;
}

function dmodel_field_suggest($f) {
	return "meta";
}

